// 缓存文件
function saveFile(asserUrl,playLine,getwords){
  console.log("getwords",getwords);
  uni.downloadFile({
    url:asserUrl ,
    success: (res) => {
    console.log(res.tempFilePath);
    let a = res.tempFilePath
        if (res.statusCode === 200) {
            console.log('下载成功');
       uni.saveFile({
            tempFilePath: a,
            success:  (res)=>{
          let savedFilePath = res.savedFilePath;
         console.log('保存成功',res,savedFilePath);
         wx.getFileSystemManager().readFile({
           filePath:savedFilePath,
           encoding:"utf8",
           success:(res)=>{
            //  console.log(res.data);
            //  console.log(res.data.split("\r\n"));
              let k = [],lrc=[]
                res.data.split("\r\n").forEach((item,index)=>{
                if(item.trim().length>0){
                  // console.log(item.split("[")[1].split("]")[0])
                  k.push(item.split("[")[1].split("]")[0])
                  if(getwords)getwords.push(item.split("]")[1])
                }
              })
              k = k.splice(4,k.length)
              lrc = lrc.splice(4,lrc.length)
              // getwords.length = 0
              // lrc.forEach((item)=>{
              //   getwords.push(item)
              // })
              console.log("lrc",getwords);
             if(playLine) timealys(k,playLine)
           },
           fail:(res)=>{
              console.log(res);
           }
         })
            },
         fail(res){
           console.log(res);
         }
          });
        // 
        }
    }
  });
//     uni.saveFile({
//     tempFilePath,
//     success:  (res)=>{
//     let  savedFilePath = res.savedFilePath;
//    console.log('保存成功',res,savedFilePath);
//    wx.getFileSystemManager().readFile({
//        filePath:savedFilePath,
//        encoding:"utf8",
//        success:(res)=>{
//            console.log(res.data);
//            console.log(res.data.split("\r\n"));
//             let k = []
//             res.data.split("\r\n").forEach((item,index)=>{
//                 if(item.trim().length>0){
//                     console.log(item.split("[")[1].split("]")[0])
//                     k.push(item.split("[")[1].split("]")[0])
//                 }
//             })
//             k = k.splice(4,k.length)
//             console.log("k",k);
//             timealys(k,playLine)
//        },
//        fail:(res)=>{
//             console.log(res);
//        }
//    })
//     },
//  fail(res){
//      console.log(res);
//  }
//   });
}
// 清除文件
function clearFile(){
  uni.getSavedFileList({
    success:(res)=>{
      console.log(res);
      res.fileList.forEach(item=>{
        uni.removeSavedFile({
          filePath:item.filePath,
          success:(res)=>{
            console.log("删除成功",res);
          },
          fail:(res)=>{
            console.log("删除失败",res);
          }
        })
      })
    }
  })
}
// 获取时间点
async function TimedopintFind(playLine,currentTime,index){
  // playLine.forEach((item,_index)=>{
  //   // fixed.push(item*1000) 
  //   if(_index==0) _index=1
  //  if(Number(playLine[_index-1])<Number(currentTime)&&Number(currentTime)<Number(item)){
  //    index = _index
  //    console.log("_index",index);
  //    console.log("_item",playLine[_index-1],currentTime,item);
  //    console.log("_item",playLine[_index-1])<Number(currentTime)&&Number(currentTime)<Number(item);
  //   } 
   
  // })
  console.log(playLine);
  let list = []
  for(let i=0;i<playLine.length;i++){
    let item = playLine[i]
    let _index = i;
    // if(_index==0) _index=1
    // if(Number(playLine[_index-1])<Number(currentTime)&&Number(currentTime)<Number(item)){
    //   index = _index
    //   console.log("_index",index);
    //   // console.log("_item",playLine[_index-1],currentTime,item);
    //   console.log("_item",Number(playLine[_index-1])<Number(currentTime)&&Number(currentTime)<Number(item));
    //  } 
    if((currentTime - item)>0){
      list.push(Math.abs(currentTime%1000 - item))
    }
    // list.push(Math.abs(currentTime - item))
  }
  console.log("list",list);
  console.log("min",list.indexOf(Math.min(...list)));
  // return  list.indexOf(Math.min(...list));
  return  list.length;
}
// 获取歌词
async function getlrc(){

}
// 拆解文件中的时间点
 function timealys(timearr,playLine){
   if(playLine.length==0){
      timearr.forEach((item,index)=>{
           let hour = item.split(":")[0];
           let min = item.split(":")[1].split(".")[0];
           let sec = item.split(":")[1].split(".")[1];
          //  console.log("hour",Number(hour));
          //  console.log("min",Number(min));
           console.log("sec",Number(sec));
           // let timeline = hour*60*69*1000+min*60*1000+sec*1000
           let timeline = Number(hour)*60+Number(min)+Number(sec/100)
           playLine.push(timeline)
         })
   }
   if(playLine.length>=0){
     playLine = []
    timearr.forEach((item,index)=>{
      let hour = item.split(":")[0];
      let min = item.split(":")[1].split(".")[0];
      let sec = item.split(":")[1].split(".")[1];
      // console.log("hour",Number(hour));
      // console.log("min",Number(min));
      // console.log("sec",Number(sec));
      // let timeline = hour*60*69*1000+min*60*1000+sec*1000
      let timeline = Number(hour)*60+Number(min)
      playLine.push(timeline)
    })
   }
   
   console.log("playLine",playLine);
  }
  export {
    saveFile,timealys,TimedopintFind,clearFile
  }